/**
 *
 * Copyright 2012 TeleNav, Inc. All rights reserved.
 * DataUtil.java
 *
 */
package com.telenav.snowsweeper.data;

import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *@author hhzhu
 *@date 2012-12-7
 */
public class DataUtil
{
    public static final String SERVER_RESPONSE_OK = "OK";
    public static final String SERVER_RESPONSE_INVALID_ROUTE = "INVALID_ROUTE";
    public static final String SERVER_RESPONSE_TIME_OUT = "TIME_OUT";
    public static final String fakeMapdata = "";
    public static final String fakePassPoint = "{\"status\": \"OK\",\"count\": 5,\"latitude\": 2.0,\"longitude\": 1.0,\"altitude\":0.0,\"speed\": 0.0,\"heading\": 0,\"edgeId\": \"\",\"timestamp\": 4000}";
    public static final String fakeHint = "{\"status\": \"OK\",\"routes\": [ {\"route\": [{ \"id\": \"01\" },{ \"id\": \"02\" },{ \"id\": \"12\" }] } ] }";
  
    public static PassPoint parsePassPoint(String response) {   
        PassPoint p = null;
        try {   
            
            JSONObject jsonObj = new JSONObject(response);
            String status = jsonObj.getString("status");   
            if(SERVER_RESPONSE_OK.equals(status))
            {
                p = new PassPoint();
                p.count = jsonObj.getInt("count");   
                p.latitude = jsonObj.getDouble("latitude");   
                p.longitude = jsonObj.getDouble("longitude"); 
            } 
        } catch (JSONException e) {   
            System.out.println("Json parse error");   
            e.printStackTrace();   
        }   
        return p;
    }  

    public static Vector<Route> parseHint(String response)
    {
        Vector<Route> result = new Vector<Route>();
        try
        {
            JSONObject jsonObj = new JSONObject(response);
            String status = jsonObj.getString("status");
            if(SERVER_RESPONSE_OK.equals(status))
            {
                JSONArray routes = jsonObj.getJSONArray("routes");
                JSONObject route = null;
                JSONObject edge = null;
                Route r = null;
                Edge e = null;
                for (int i = 0; i < routes.length(); i++)
                {
                    route = routes.getJSONObject(i);
                    if (route != null)
                    {
                        r = new Route();
                        JSONArray edges = route.getJSONArray("route");
                        for (int j = 0; j < routes.length(); j++)
                        {
                            edge = edges.getJSONObject(j);
                            if (edge != null)
                            {
//                                e = new Edge();
//                                e.id = edge.getString("id");
//                                r.addEdges(e);
                            }
                        }
                        result.add(r);
                    }
                }
            }
        } catch (JSONException e)
        {
            e.printStackTrace();
        }
        return result;
    }
    
}
